const bcrypt = require('bcryptjs')
const userCheck = async (ctx, next) => {
    // TODO: write the code
    const { username, password } = ctx.request.body
    // 如果用户名或者密码为空
    if (!username || !password) {
        ctx.status = 500;
        ctx.body = { code: 500, message: '用户名或密码不能为空!' };
        return;
    }
    await next()
}

const passwordBcryptjs = async (ctx, next) => {
    try {
        var { password } = ctx.request.body
        const salt = bcrypt.genSaltSync(10); //密码js加密
        const passwordHash = bcrypt.hashSync(password, salt); //密码加密
        ctx.request.body.password = passwordHash //将加密后的密码放入request.body中
    } catch (err) {
        ctx.status = 500;
        ctx.body = { code: 500, message: err.message };
        return
    }
    await next()
}

module.exports = {
    userCheck,
    passwordBcryptjs
}